Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
mongodb - match if a group of values from an array is in another given array [with Aggregation]

I'm looking for a solution since 3 hours and I don't get it yet.

I have the following collection:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 2', indexes:[1,3] },
{text: 'random text 3', indexes:[2,4] },

and I would like to have only the documents that have all the index values in a given array like for example [1,2,4]

using the example above, I would like to have the following output:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 3', indexes:[2,4] },

[1,2] is in [1,2,4] -> OK

[1,3] is not in [1,2,4] because of 3 -> Not OK

[1,4] is in [1,2,4] -> OK

Any idea? thanks for your answer! :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use one of this this $match stage:

  • Using $setIsSubset into $expr:
{
  "$match": {
    "$expr": {
      "$setIsSubset": ["$indexes",[1,2,4]]
    }
  }
}

Example here

  • Using $elemMatch and double negation ($not and $nin):
{
  "$match": {
    "indexes": {
      "$not": {
        "$elemMatch": {
          "$nin": [1,2,4]
        }
      }
    }
  }
}

Example here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda